home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.02 Feb 97 / Getting Started, ShapeWorld / Schematic Files / Schematic.java < prev    next >
Encoding:
Java Source  |  1996-11-05  |  3.9 KB  |  160 lines  |  [TEXT/CWIE]

  1. import java.awt.*;
  2. import java.lang.*;
  3. import java.util.*;
  4.  
  5. abstract class Device {
  6.     Schematic schematic;
  7.     Rectangle boundingBox;
  8.     boolean highlighted;
  9.  
  10.     Device(Schematic s, Point p) {
  11.         schematic = s;
  12.         highlighted= false;
  13.     }
  14.  
  15.     abstract public void draw(Graphics g);
  16.  
  17.     public boolean pick(Point p) {
  18.         return boundingBox.inside(p.x, p.y);
  19.     }
  20.  
  21.     public boolean isHighlighted() {
  22.         return highlighted;
  23.     }
  24.  
  25.     public void setHighlight(boolean b) {
  26.         highlighted = b;
  27.     }
  28.  
  29.     public void setPosition(Point p) {
  30.         boundingBox.move(p.x, p.y);
  31.     }
  32.  
  33.     public Point getPosition() {
  34.         return new Point(boundingBox.x, boundingBox.y);
  35.     }
  36.  
  37.     public Rectangle getBoundingBox() {
  38.         return boundingBox;
  39.     }
  40. }
  41.  
  42. class Resistor extends Device {
  43.  
  44.     Resistor(Schematic s, Point p) {
  45.         super(s, p);
  46.         boundingBox = new Rectangle(p.x, p.y, 25, 100);
  47.     }
  48.  
  49.     public void draw(Graphics g) {
  50.         g.setColor(Color.black);
  51.  
  52.         int x = boundingBox.x;
  53.         int y = boundingBox.y;
  54.         int width = boundingBox.width;
  55.         int height = boundingBox.height;
  56.         int halfWidth = boundingBox.width/2;
  57.         int quarterHeight = boundingBox.height/4;
  58.  
  59.         g.drawLine(x+halfWidth, y, x+halfWidth, y+quarterHeight);
  60.         if (highlighted)
  61.             g.fillRect(x, y+quarterHeight, width, quarterHeight*2);
  62.         else
  63.             g.drawRect(x, y+quarterHeight, width, quarterHeight*2);
  64.         g.drawLine(x+halfWidth, y+quarterHeight*3,
  65.                    x+halfWidth, y+height);
  66.     }
  67. }
  68.  
  69. class Schematic extends Canvas {
  70.     Vector devices;
  71.     Device pickedDevice;
  72.     Point offset;
  73.  
  74.     Schematic() {
  75.         devices = new Vector();
  76.         pickedDevice = null;
  77.     }
  78.  
  79.     void addDevice(Device d) {
  80.         devices.addElement(d);
  81.     }
  82.      
  83.     void removeDevice(Device d) {
  84.         devices.removeElement(d);
  85.     }   
  86.  
  87.     public void paint(Graphics g) {
  88.         for (Enumeration e = devices.elements();
  89.                                 e.hasMoreElements(); ) {
  90.             Device d = (Device) e.nextElement();
  91.             d.draw(g);
  92.         }
  93.     }
  94.  
  95.     public Device pickDevice(Point p) {
  96.         for (Enumeration e = devices.elements();
  97.                                 e.hasMoreElements(); ) {
  98.             Device d = (Device) e.nextElement();
  99.             if (d.pick(p)) {
  100.                 d.setHighlight(!d.isHighlighted());
  101.                 repaint();
  102.                 return d;
  103.             }
  104.         }
  105.         return null;
  106.     }
  107.  
  108.     public boolean mouseDown(Event e, int x, int y) {
  109.         pickedDevice = pickDevice(new Point(x, y));
  110.         Point p = pickedDevice.getPosition();
  111.         offset = new Point(p.x-x, p.y-y);
  112.         return true;
  113.     }
  114.  
  115.     public boolean mouseDrag(Event e, int x, int y) {
  116.         if (pickedDevice != null) {
  117.             pickedDevice.setPosition( new Point(x+offset.x,
  118.                                                 y+offset.y) );
  119.             repaint();
  120.             return true;
  121.         }
  122.         return false;
  123.     }
  124.  
  125.     public boolean mouseUp(Event e, int x, int y) {
  126.         if (pickedDevice != null) {
  127.             pickedDevice.setPosition( new Point(x+offset.x,
  128.                                                 y+offset.y) );
  129.             pickedDevice.setHighlight(false);
  130.             repaint();
  131.             pickedDevice = null;
  132.             return true;
  133.         }
  134.         return false;
  135.     }
  136. }
  137.  
  138. public class SchematicTest extends Frame {
  139.     Schematic s;
  140.  
  141.     public SchematicTest() {
  142.  
  143.         super("Sample Electronics Schematic");
  144.         setLayout( new BorderLayout());
  145.         add("Center", s = new Schematic());
  146.  
  147.         s.addDevice( new Resistor(s, new Point(20, 100)));
  148.         s.addDevice( new Resistor(s, new Point(130, 100)));
  149.         s.addDevice( new Resistor(s, new Point(250, 100)));
  150.  
  151.         resize(300, 300);
  152.         show();
  153.    }
  154.  
  155.     public static void main(String args[]) {
  156.     Frame f = new SchematicTest();
  157.     }
  158. }
  159.  
  160.